import Vue from "vue";
import VueRouter from "vue-router";
import VueRouterTitle from "vue-router-title";
import routes from "./router";
import store from "./store";
import Mint from "mint-ui";
import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";
import LoadScript from "vue-load-script";
// import VeeValidate from 'vee-validate';
import "mint-ui/lib/style.css";
import "common/rem";
import FastClick from "fastclick";
import { getCookie, addCookie } from "common/utils";
import filters from "common/filter";
import components from "common/components";
// import { BASE_API, wxAuthUrl } from 'service/env'
import { whiteList } from "./router/whiteList";

import {
  getToken,
  setToken,
  getOpenID,
  setOpenID,
  getWxAuth,
  setTag,
  getTag,
} from "service/auth";
import { wxshare } from "service/share";

// import './mock/index.js'; // 该项目所有请求使用mockjs模拟

if ("addEventListener" in document) {
  document.addEventListener(
    "DOMContentLoaded",
    function () {
      FastClick.attach(document.body);
    },
    false
  );
}
Vue.config.productionTip = false;

Vue.use(Mint);
Vue.use(ElementUI);
Vue.use(VueRouter);
Vue.use(LoadScript);
// Vue.use(VeeValidate);
const router = new VueRouter({
  routes,
  mode: "history",
  strict: process.env.NODE_ENV !== "production",
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition;
    } else {
      if (from.meta.keepAlive) {
        from.meta.savedPosition = document.body.scrollTop;
      }
      return { x: 0, y: to.meta.savedPosition || 0 };
    }
  },
});

// 标题修改
VueRouterTitle({
  router: router,
  defaultTitle: "脂谱身心健康",
});

router.beforeEach((to, from, next) => {
  // zhuge.track(to.meta.title);
  if (to.query.openid) {
    setOpenID(to.query.openid);
    store.commit("SET_OPENID", to.query.openid);
  }
  if (to.query.token) {
    setToken(to.query.token);
    store.commit("SET_TOKEN", to.query.token);
    let tpAdminCode = to.query.tpAdminCode;
    let code = to.query.code;
    let adminCode = to.query.adminCode;
    let mac = to.query.mac;
    let base = `${to.path}?`;
    if (tpAdminCode) {
      base = `${base}tpAdminCode=${tpAdminCode}&`;
    }
    if (mac) {
      base = `${base}mac=${mac}&`;
    }
    if (adminCode) {
      base = `${base}adminCode=${adminCode}&`;
    }
    if (code) {
      base = `${base}code=${code}&`;
    }
    location.replace(base);
  }

  if (whiteList.indexOf(to.path) !== -1) {
    next();
  } else if (getToken() && getOpenID()) {
    if (to.meta.isChannel && !store.getters.phone) {
      next("/doctor/loginPhone");
    }
    if (
      !store.getters.uid 
    ) {
      store.dispatch("GetInfo").then((res) => {
        next();
      });
    } else {
      next();
    }
  } else {
    //wx chat auth
    getWxAuth();
  }
});

// router.afterEach(route => {
//   wxshare();
//   // if (whiteList.indexOf(route.path) == -1) {
//   //   setTimeout(function() { wxshare() }, 1000);
//   // }
// })

new Vue({
  router,
  store,
}).$mount("#app");
